package com.stupro.cjlu.manager;

import com.stupro.cjlu.common.vo.StudentItemVO;
import com.stupro.cjlu.common.vo.IDNameVO;
import com.stupro.cjlu.dao.StudentCourseDAO;
import com.stupro.cjlu.dao.StudentsDAO;
import com.stupro.cjlu.entity.Students;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StudentsManager extends BaseManager{
    private final StudentCourseDAO studentCourseDAO;
    private final StudentsDAO studentDAO;

    public StudentsManager(StudentCourseDAO studentCourseDAO, StudentsDAO studentDAO) {
        this.studentCourseDAO = studentCourseDAO;
        this.studentDAO = studentDAO;
    }

    public Integer getPageCount(String className, String name) {
        int count = studentDAO.count(className, name);
        return PageCount(count, StudentsDAO.PAGE_SIZE);
    }

    public List<StudentItemVO> getPage(Integer index, String className, String name) {
        return studentDAO.getPage(index, className, name);
    }
    public List<StudentItemVO> getAll(Integer index){return  studentDAO.getPage(index,null,null);}

    public Students get(Integer id) {
        return studentDAO.get(id);
    }

    public int create(Students entity) {
        return studentDAO.insert(entity);
    }

    public int update(Students entity) {
        return studentDAO.update(entity);
    }

    public int delete(Integer id) {
        return studentDAO.delete(id);
    }

    public boolean hasStudentCourse(Integer studentId) {
        return studentCourseDAO.countByStudentId(studentId) > 0;
    }

    public List<IDNameVO> listName() {
        List<IDNameVO> voList = new ArrayList<>();
        List<Students> entityList = studentDAO.listName();
        for (Students entity : entityList) {
            voList.add(new IDNameVO(entity.getStuID(), entity.getStuName()));
        }

        return voList;
    }
}
